package com.hilihili.transcode;

import com.hilihili.config.TranscodeConfig;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 视频转码 CUDA加速命令
 *
 * @author machi
 * @date 2024/01/31
 */
public class CUDACommand extends AbstractTranscodeCommand {
    private TranscodeConfig transcodeConfig;

    public CUDACommand(TranscodeConfig transcodeConfig) {
        this.transcodeConfig = transcodeConfig;
    }

    @Override
    List<String> getTranscodeCommand(String ffmpeg) {
        List<String> commands = new ArrayList<>();
        //ffmpeg  -c:v h264_cuvid -i input.mp4
        // -filter_complex
        // "[0:v]split=4[s0][s1][s2][s3];[s0]scale=1920x1080:force_original_aspect_ratio=decrease[0v];[s1]scale=1280x720:force_original_aspect_ratio=decrease[1v];[s2]scale=858x480:force_original_aspect_ratio=decrease[2v];[s3]scale=630x360:force_original_aspect_ratio=decrease[3v]"
        // -map "[0v]" -map 0:1 -map "[1v]" -map 0:1 -map "[2v]" -map 0:1 -map "[3v]" -map 0:1 -b:v:0 1800k -b:v:1 1200k -b:v:2 750k -b:v:3 550k -c:v h264_nvenc -c:a copy -var_stream_map "v:0,a:0,name:1080p v:1,a:1,name:720p v:2,a:2,name:480p v:3,a:3,name:360p"
        // -master_pl_name index.m3u8 -f hls -hls_time 10 -hls_playlist_type vod -hls_list_size 0
        // -hls_segment_filename "%v/%06d.ts" %v/index.m3u8
        commands.add(ffmpeg);
        commands.add("-c:v");
        commands.add("h264_cuvid");
        commands.add("-i");
        commands.add(getSource());
        commands.add("-filter_complex");
        commands.add("\"[0:v]split=4[s0][s1][s2][s3];[s0]scale=1920x1080:force_original_aspect_ratio=decrease[0v];[s1]scale=1280x720:force_original_aspect_ratio=decrease[1v];[s2]scale=858x480:force_original_aspect_ratio=decrease[2v];[s3]scale=630x360:force_original_aspect_ratio=decrease[3v]\"");
        commands.addAll(Arrays.asList("-map", "[0v]", "-map", "0:1",
                "-map", "[1v]", "-map", "0:1",
                "-map", "[2v]", "-map", "0:1",
                "-map", "[3v]", "-map", "0:1",
                "-b:v:0", "1800k", "-b:v:1", "1200k", "-b:v:2", "750k", "-b:v:3", "550k"));
        commands.add("-c:v");
        commands.add("h264_nvenc");
        commands.add("-c:a");
        commands.add("copy");
        commands.add("-var_stream_map");
        commands.add("v:0,a:0,name:1080p v:1,a:1,name:720p v:2,a:2,name:480p v:3,a:3,name:360p");
        commands.add("-master_pl_name");
        commands.add("index.m3u8");
        commands.add("-hls_key_info_file");
        // 指定密钥文件路径
        commands.add(getKeyInfo());
        commands.add("-f");
        commands.add("hls");
        commands.add("-hls_time");
        commands.add(transcodeConfig.getTsSeconds());
        commands.add("-hls_playlist_type");
        commands.add("vod");
        commands.add("-hls_list_size");
        commands.add("0");
        commands.add("-hls_segment_filename");
        commands.add("\"%v/%06d.ts\"");
        commands.add("%v/index.m3u8");
        return commands;

    }

}
